package com.hc.utils;

import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.symmetric.SymmetricAlgorithm;
import cn.hutool.crypto.symmetric.SymmetricCrypto;

/**
 * 加解密
 * @author hecai
 */
public class CryptoUtil {
    /**
     * 随机生成密钥
     */
    private static byte[] key = SecureUtil.generateKey(SymmetricAlgorithm.AES.getValue()).getEncoded();

    /**
     * 构建
     */
    private static SymmetricCrypto aes = new SymmetricCrypto(SymmetricAlgorithm.AES,key);

    /**
     * 加密
     * @param source 原文
     * @param key 密钥
     * @return
     */
    public static String encode(String source,byte[] key){
        //加密
        byte[] encrypt = aes.encrypt(source);
        return new String(encrypt);
    }
    /**
     * 解密
     * @param encrypt 密文
     * @param key 密钥
     * @return
     */
    public static String decode(String encrypt,byte[] key){
        //加密
        byte[] decrypt = aes.decrypt(encrypt);
        return new String(decrypt);
    }

    public static void main(String[] args) {
        final String encode = encode("a中国b", key);
        System.out.println(encode);
        final String decode = decode(encode, key);
        System.out.println(decode);
    }




}